package com.ruoyi.order.service.impl;

import java.time.LocalDateTime;
import java.time.temporal.WeekFields;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.order.mapper.SalesOrderMapper;
import com.ruoyi.order.domain.SalesOrder;
import com.ruoyi.order.service.ISalesOrderService;

/**
 * 订单管理Service业务层处理
 * 
 * @author lzp
 * @date 2024-12-29
 */
@Service
public class SalesOrderServiceImpl implements ISalesOrderService 
{
    @Autowired
    private SalesOrderMapper salesOrderMapper;

    /**
     * 查询订单管理
     * 
     * @param orderId 订单管理主键
     * @return 订单管理
     */
    @Override
    public SalesOrder selectSalesOrderByOrderId(String orderId)
    {
        return salesOrderMapper.selectSalesOrderByOrderId(orderId);
    }

    /**
     * 查询订单管理列表
     * 
     * @param salesOrder 订单管理
     * @return 订单管理
     */
    @Override
    public List<SalesOrder> selectSalesOrderList(SalesOrder salesOrder)
    {
        return salesOrderMapper.selectSalesOrderList(salesOrder);
    }

    /**
     * 新增订单管理
     * 
     * @param salesOrder 订单管理
     * @return 结果
     */
    @Override
    public int insertSalesOrder(SalesOrder salesOrder)
    {
        return salesOrderMapper.insertSalesOrder(salesOrder);
    }

    /**
     * 修改订单管理
     * 
     * @param salesOrder 订单管理
     * @return 结果
     */
    @Override
    public int updateSalesOrder(SalesOrder salesOrder)
    {
        return salesOrderMapper.updateSalesOrder(salesOrder);
    }

    /**
     * 批量删除订单管理
     * 
     * @param orderIds 需要删除的订单管理主键
     * @return 结果
     */
    @Override
    public int deleteSalesOrderByOrderIds(String[] orderIds)
    {
        return salesOrderMapper.deleteSalesOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单管理信息
     * 
     * @param orderId 订单管理主键
     * @return 结果
     */
    @Override
    public int deleteSalesOrderByOrderId(String orderId)
    {
        return salesOrderMapper.deleteSalesOrderByOrderId(orderId);
    }



    @Override
    public List<Map<String, Object>> selectOrderStatusCount()
    {
        return salesOrderMapper.selectOrderStatusCount();
    }

    @Override
    public List<Map<String, Object>> selectCustomerOrderStatusCount(String customerId)
    {
        return salesOrderMapper.selectCustomerOrderStatusCount(customerId);
    }

    @Override
    public List<Map<String, Object>> getDailyOrderCount(Map<String, String> params) {
        return salesOrderMapper.selectDailyOrderCount(params);
    }

    @Override
    public Double selectTodayTotalSales()
    {
        LocalDateTime startOfDay = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime endOfDay = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59).withNano(999999999);
        return salesOrderMapper.selectTotalSalesByDateRange(startOfDay, endOfDay);
    }

    /**
     * 查询本周的销售总额
     *
     * @return 销售总额
     */
    @Override
    public Double selectThisWeekTotalSales()
    {
        LocalDateTime now = LocalDateTime.now();
        WeekFields weekFields = WeekFields.of(Locale.getDefault());
        int week = now.get(weekFields.weekOfWeekBasedYear());
        int year = now.get(weekFields.weekBasedYear());

        LocalDateTime startOfWeek = now.with(weekFields.weekOfWeekBasedYear(), week)
                .with(weekFields.dayOfWeek(), 1)
                .withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime endOfWeek = startOfWeek.plusDays(6)
                .withHour(23).withMinute(59).withSecond(59).withNano(999999999);
        return salesOrderMapper.selectTotalSalesByDateRange(startOfWeek, endOfWeek);
    }

    /**
     * 查询本月的销售总额
     *
     * @return 销售总额
     */
    @Override
    public Double selectThisMonthTotalSales()
    {
        LocalDateTime startOfMonth = LocalDateTime.now().withDayOfMonth(1)
                .withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime endOfMonth = startOfMonth.plusMonths(1).minusDays(1)
                .withHour(23).withMinute(59).withSecond(59).withNano(999999999);
        return salesOrderMapper.selectTotalSalesByDateRange(startOfMonth, endOfMonth);
    }

    @Override
    public int selectTodayOrderCount()
    {

        return salesOrderMapper.selectTodayOrderCount();
    }

}
